package com.zhongyu.controller;

import com.zhongyu.pojo.Order;
import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.pojo.Provider;
import com.zhongyu.pojo.User;
import com.zhongyu.service.OrderDetailService;
import com.zhongyu.service.OrderService;
import com.zhongyu.service.ProviderService;
import com.zhongyu.vo.CgOrderVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/caigou/cgddcx")
public class CgOrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private ProviderService providerService;

    @GetMapping("/search3/{pageNo}/{pageSize}")
    public ResultVo search3(@PathVariable Integer pageNo, @PathVariable Integer pageSize, CgOrderVo cgOrderVo) {
        PageResult pageResult = orderService.search(pageNo, pageSize,cgOrderVo );
        return ResultVo.success(pageResult);
    }
    @GetMapping("/search/{pageNo}/{pageSize}")
    @RequiresPermissions("cgddqr:list")
    public ResultVo search(@PathVariable Integer pageNo, @PathVariable Integer pageSize, CgOrderVo cgOrderVo) {
        PageResult pageResult = orderService.search(pageNo, pageSize,cgOrderVo );
        return ResultVo.success(pageResult);
    }
    @GetMapping("/search1/{pageNo}/{pageSize}")
    @RequiresPermissions("cgddsh:list")
    public ResultVo search1(@PathVariable Integer pageNo, @PathVariable Integer pageSize, CgOrderVo cgOrderVo) {
        PageResult pageResult = orderService.search(pageNo, pageSize,cgOrderVo );
        return ResultVo.success(pageResult);
    }
    @GetMapping("/search2/{pageNo}/{pageSize}")
    @RequiresPermissions("cgddrk:list")
    public ResultVo search2(@PathVariable Integer pageNo, @PathVariable Integer pageSize, CgOrderVo cgOrderVo) {
        PageResult pageResult = orderService.search(pageNo, pageSize,cgOrderVo );
        return ResultVo.success(pageResult);
    }
    @GetMapping("/list")
    public ResultVo search(Integer id) {
        List<OrderDetail> list = orderDetailService.findById(id);
        return ResultVo.success(list);
    }
    @PostMapping("/ruku")
    public ResultVo doStore(@RequestBody OrderDetail orderDetail, HttpSession session) {
        try {
            User loginUser = (User) session.getAttribute("loginUser");
            Integer detailId = orderDetail.getId();
            Integer cangkuId = orderDetail.getStorehouseId();
            //  orderDetailService.doOutStore(loginUser.getId(), detailId, cangkuId);
            return   orderDetailService.doStore(loginUser.getId(), detailId, cangkuId);
        } catch (Exception e) {
            return ResultVo.failure("入库失败", e);
        }
    }
    @PostMapping("/add")
    public ResultVo tianCai(@RequestBody Order order, HttpSession session){
        try{
            if(order.getProviderId()==null){
                return ResultVo.failure("请选择供应商");
            }
            User loginUser= (User) session.getAttribute("loginUser");
            order.setCreator(loginUser.getId());
            return    orderService.tianCai(order);
        }catch (Exception ex){
            return ResultVo.failure("添加采购订单失败",ex);
        }
    }

    @GetMapping("/del")
    public ResultVo del(Integer id){
        orderService.del(id);
        return null;
    }
}
